package com.vitogiovanni.tobook.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.vitogiovanni.tobook.entity.LocaleEndpoint;
import com.vitogiovanni.tobook.entity.OrariApertura;

/**
 * Servlet implementation class InvitaLavoroServlet
 */
public class InserisciOrarioAperturaServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public InserisciOrarioAperturaServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

	}

	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		
		//OrariAperturaEndpoint orariAperturaEndpoint = new OrariAperturaEndpoint();
		
		LocaleEndpoint localeEndpoint = new LocaleEndpoint();
		
		String localeId = req.getParameter("localeId");
		String orarioApertura = req.getParameter("orarioApertura");
		String orarioChiusura = req.getParameter("orarioChiusura");
		
		long oraAp = Long.parseLong(orarioApertura.substring(0, 2))*60*60*1000;
		long minAp = Long.parseLong(orarioApertura.substring(3, 5))*60*1000;
		
		long oraCh = Long.parseLong(orarioChiusura.substring(0, 2))*60*60*1000;
		long minCh = Long.parseLong(orarioChiusura.substring(3, 5))*60*1000;
		if(oraCh+minCh <= oraAp + minAp)
		{
			oraCh = oraCh+1000*60*60*24;
		}
		OrariApertura orariApertura = new OrariApertura(oraAp+minAp, oraCh+minCh);
		localeEndpoint.insertOrarioLocale(orariApertura, localeId);
		
		resp.sendRedirect("/inserimento.html");
	}
}